import { ResponseUtilService } from '@app/response-util';
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, Req } from '@nestjs/common';
import { ReqAttrs } from '../../constant';
import { UserGroupService } from './user-group.service';
import { UserGroup } from '@app/db/entity/UserGroup';

@Controller('userGroup')
export class UserGroupController {
    constructor(
        private readonly service:UserGroupService
    ){}

    @Get('options')
    async options() {
        const rows = await this.service.options()
        return ResponseUtilService.successRes(rows)
    }
    
    @Get('list')
    async list() {
        const rows = await this.service.list()
        return ResponseUtilService.successRes(rows)
    }


    @Post('add')
    async add(@Body() dto:UserGroup, @Req() req) {
        try {
            const r = await this.service.add(dto, req[ReqAttrs.MEMBER_ID])
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('update')
    async update(@Body() dto:UserGroup, @Req() req) {
        try {
            const r = await this.service.update(dto, req[ReqAttrs.MEMBER_ID])
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        }
    }

    @Post('delete')
    async del(@Body('id', ParseIntPipe) id:number) {
        if(id < 1) return null
        try {
            const r = await this.service.del(id)
            return r ? ResponseUtilService.successRes() : ResponseUtilService.failRes()
        } catch (error) {
            return ResponseUtilService.failRes(error.message)
        } 
    }
}
